package com.itqf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.bean.DtsAdmin;
import com.itqf.dto.DtsSearchDto;
import com.itqf.mapper.DtsAdminMapper;
import com.itqf.service.DtsAdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: sm_project
 *
 * @author: 赵伟风
 * time: 2021/7/13 14:26
 * description:用户模块业务实现类
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    private DtsAdminMapper dtsAdminMapper;

    public static final Logger logger = LoggerFactory.getLogger(DtsAdminServiceImpl.class);

    /**
     * 根据用户名查询用户的实体对象
     *
     * @param username
     * @return
     */
    @Override
    public DtsAdmin queryByUsername(String username) {

        if (username == null) {
            //warn
            logger.warn("[账号查看用户业务]username为null!");
            return null;
        }

        /**
         * 查询数据库!
         */
        DtsAdmin dtsAdmin = dtsAdminMapper.queryByUserName(username);

        return dtsAdmin;
    }

    @Override
    public Map findAdminByPage(DtsSearchDto dto) {

        logger.info("[管理员查询]业务开始调用,分页参数为:{}", dto);

        //1.分页插件启动
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        //2.数据库查询
        List<DtsAdmin> adminList = dtsAdminMapper.queryAdminByPage(dto);
        //3.pageInfo封装
        PageInfo<DtsAdmin> pageInfo = new PageInfo<>(adminList);
        //4.封装返回结果数据

        Map data = new HashMap();
        data.put("total", pageInfo.getTotal());
        data.put("items", pageInfo.getList());


        logger.info("[管理员查询]业务调用完毕,获取数据为:{}", data);

        return data;
    }

    @Override
    public boolean checkUserName(String username) {

        Long count = dtsAdminMapper.queryCountByUserName(username);

        logger.debug("[用户查询]username={}的账号存在数量为:{}!", username, count);

        return count >= 1;
    }

    @Override
    public DtsAdmin saveAdmin(DtsAdmin admin) {

        //1.完善admin的数据
        //1.补全没有的数据
        admin.setAddTime(new Date());
        admin.setDeleted(false);
        //2.需要修改的数据

        logger.debug("[保存管理员]业务内部获取的用户密码为:{}", admin.getPassword());
        //2.插入数据库
        int rows = dtsAdminMapper.insertAdmin(admin);
        //3.返回结果
        return admin;
    }


    @Override
    public int updateAdmin(DtsAdmin dtsAdmin) {

        dtsAdmin.setUpdateTime(new Date());

        int rows = dtsAdminMapper.updateById(dtsAdmin);

        logger.debug("[修改管理员]业务修改数据的影响行数rows:{}",rows);

        return rows;
    }

    @Override
    public int removeAdminById(Integer id) {

        int rows = dtsAdminMapper.deleteByPrimaryKey(id);

        return rows;
    }
}
